package com.time.hellotime.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;

import com.time.hellotime.app.AppConfig;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * =====================================
 * 作    者: 陈嘉桐
 * 版    本：1.1.4
 * 创建日期：2017/4/25
 * 描    述：
 * =====================================
 */
public class ImageUtils {

    public static String saveBitmap(Context context, Bitmap b) {
        String path = AppConfig.getInstance().APP_PATH_ROOT;

        File appDir = new File(path);
        if (!appDir.exists()) {
            appDir.mkdir();
        }

        long dataTake = System.currentTimeMillis();
        String jpegName = path + File.separator + "picture_" + dataTake + ".jpg";

        File file = new File(appDir, jpegName);
        if (file.exists()) {
            file.delete();
        }

        try {
            FileOutputStream fos = new FileOutputStream(jpegName);
            boolean isSuccess = b.compress(Bitmap.CompressFormat.JPEG, 80, fos);
            fos.flush();
            CloseUtils.close(fos);

            //保存图片后发送广播通知更新数据库
//            Uri uri = FileProvider7.getUriForFile(context, file);
            Uri uri = Uri.fromFile(file);
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));

            if (isSuccess) {
                ToastUtils.showShort("图片已保存成功");
            } else {
                ToastUtils.showShort("图片已保存失败");
            }

            return jpegName;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean deleteFile(String url) {
        boolean result = false;
        File file = new File(url);
        if (file.exists()) {
            result = file.delete();
        }
        return result;
    }

    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    /*
        * 获取网络图片
        * 保存到本地
        * */
    public static void savePicPath(final Context context, String picUrl, String pathName) {
        // 最后获取的本地路径名
        final String path = AppConfig.getInstance().APP_PATH_ROOT + File.separator + pathName;
        final File file = new File(path);
        Request<Bitmap> imageRequest = NoHttp.createImageRequest(picUrl);
        NoHttp.newRequestQueue().add(490, imageRequest, new OnResponseListener<Bitmap>() {
            @Override
            public void onStart(int what) {

            }

            @Override
            public void onSucceed(int what, Response<Bitmap> response) {
                Bitmap bitmap = response.get();
                OutputStream os = null;
                try {
                    os = new FileOutputStream(path);
                    if (bitmap != null) {
                        boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
                        os.flush();
                        if (isSuccess) {
                            ToastUtils.showShort("图片已保存成功");
                        } else {
                            ToastUtils.showShort("图片已保存失败");
                        }
                    }
                } catch (Exception e) {

                } finally {
                    CloseUtils.close(os);
                    //保存图片后发送广播通知更新数据库
                    Uri uri = Uri.fromFile(file);
                    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
                }

            }

            @Override
            public void onFailed(int what, Response<Bitmap> response) {

            }

            @Override
            public void onFinish(int what) {

            }
        });
    }

}
